package com.unitd.base.frame.plugin.db.datasource.properties;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.*;
import lombok.experimental.Accessors;

import java.sql.SQLException;
import java.util.Properties;

/**
 * Druid数据库连接池基础参数信息定义类
 *
 * @author hudan
 * @version 1.0
 * @filename DruidProperties.java
 * @copyright www.unitd.com
 * @date 2020-07-08
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ToString(callSuper = true)
@EqualsAndHashCode
public class DruidProperties {

    private String url;
    private String username;
    private String password;
    private String driverClassName;
    private Integer initialSize = 2;
    private Integer minIdle = 1;
    private Integer maxActive = 20;
    private Integer maxWait = 60000;
    private Integer timeBetweenEvictionRunsMillis = 60000;
    private Integer minEvictableIdleTimeMillis = 300000;
    private String validationQuery;
    private Boolean testWhileIdle = true;
    private Boolean testOnBorrow = true;
    private Boolean testOnReturn = true;
    private Boolean poolPreparedStatements = true;
    private Integer maxPoolPreparedStatementPerConnectionSize = 20;
    private String filters = "stat";
    private String dataSourceName;

    public void config(DruidDataSource dataSource) {
        dataSource.setUrl(this.url);
        dataSource.setUsername(this.username);
        dataSource.setPassword(this.password);
        dataSource.setDriverClassName(this.driverClassName);
        dataSource.setInitialSize(this.initialSize);
        dataSource.setMinIdle(this.minIdle);
        dataSource.setMaxActive(this.maxActive);
        dataSource.setMaxWait((long) this.maxWait);
        dataSource.setTimeBetweenEvictionRunsMillis((long) this.timeBetweenEvictionRunsMillis);
        dataSource.setMinEvictableIdleTimeMillis((long) this.minEvictableIdleTimeMillis);
        dataSource.setValidationQuery(this.getValidateQueryByUrl(this.url));
        dataSource.setTestWhileIdle(this.testWhileIdle);
        dataSource.setTestOnBorrow(this.testOnBorrow);
        dataSource.setTestOnReturn(this.testOnReturn);
        dataSource.setPoolPreparedStatements(this.poolPreparedStatements);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(this.maxPoolPreparedStatementPerConnectionSize);

        try {
            dataSource.setFilters(this.filters);
        } catch (SQLException var3) {
            var3.printStackTrace();
        }
    }

    public Properties createProperties() {
        Properties properties = new Properties();
        properties.put("url", this.url);
        properties.put("username", this.username);
        properties.put("password", this.password);
        properties.put("driverClassName", this.driverClassName);
        properties.put("initialSize", this.initialSize);
        properties.put("maxActive", this.maxActive);
        properties.put("minIdle", this.minIdle);
        properties.put("maxWait", this.maxWait);
        properties.put("poolPreparedStatements", this.poolPreparedStatements);
        properties.put("maxPoolPreparedStatementPerConnectionSize", this.maxPoolPreparedStatementPerConnectionSize);
        properties.put("validationQuery", this.getValidateQueryByUrl(this.url));
        properties.put("testOnBorrow", this.testOnBorrow);
        properties.put("testOnReturn", this.testOnReturn);
        properties.put("testWhileIdle", this.testWhileIdle);
        properties.put("timeBetweenEvictionRunsMillis", this.timeBetweenEvictionRunsMillis);
        properties.put("minEvictableIdleTimeMillis", this.minEvictableIdleTimeMillis);
        properties.put("filters", this.filters);
        return properties;
    }

    private String getValidateQueryByUrl(String url) {
        if (url.contains("oracle")) {
            return "select 1 from dual";
        } else if (url.contains("postgresql")) {
            return "select version()";
        } else {
            return url.contains("sqlserver") ? "select 1" : "select 1";
        }
    }
}